//
//  HomeMainIconItemCell.m
//  PPDL
//
//  Created by 王洋 on 2019/8/21.
//  Copyright © 2019 王洋. All rights reserved.
//

#import "HomeMainIconItemCell.h"
#import "HomeMainIconItemCollectionCell.h"

@interface HomeMainIconItemCell ()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong) UICollectionView *collectionView;

@property (nonatomic, assign) CGSize       cellSize;
@property (nonatomic, assign) UIEdgeInsets collectionEdge;


@end

@implementation HomeMainIconItemCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.backgroundColor = PP_Default_Color_Gray_F1F1F1;
        [self layout];
    }
    return self;
}

- (void)layout {
    
    [self addSubview:self.collectionView];
    
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.mas_equalTo(0);
    }];
    
    CGFloat width = TTScreenWidth;
    CGFloat cellWidth;
    //    CGFloat distance = 5;
    cellWidth = (width - 4)/4;
    
    _cellSize =  CGSizeMake(cellWidth, cellWidth * PP_Home_MainIcon_Scale);
    _collectionEdge = UIEdgeInsetsMake(PP_Home_MainIcon_Distance, PP_Home_MainIcon_Distance, PP_Home_MainIcon_Distance, PP_Home_MainIcon_Distance);
    

}

- (void)layoutIfNeeded {
    [super layoutIfNeeded];
    
    [self.collectionView pp_clipCorners:UIRectCornerBottomLeft|UIRectCornerBottomRight radius:10];
}

- (void)fillCellWithModel:(HomeHeadListModel *)model {
    
    self.model = model;
    //赋值
    [self.collectionView reloadData];

}

#pragma mark delegate && dataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.model.itemArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    HomeMainIconItemCollectionCell *cell;
    
    if (!cell) {
        cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HomeMainIconItemCollectionCellID" forIndexPath:indexPath];
    }

    [cell fillCellWithModel:self.model.itemArray[indexPath.row]];
    
    return cell;
}

-(CGSize)collectionView:(UICollectionView *)collectionView
                 layout:(UICollectionViewLayout *)collectionViewLayout
 sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    return _cellSize;
}

//设置cell的位置
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView
                       layout:(UICollectionViewLayout *)collectionViewLayout
       insetForSectionAtIndex:(NSInteger)section
{
    return _collectionEdge;
}

//最小行间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 1;
}

//最小列间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 1;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (self.MainIconClickedBlock) {
        self.MainIconClickedBlock(indexPath.row);
    }
}


- (UICollectionView *)collectionView {
    
    if (!_collectionView) {
        
        UICollectionViewFlowLayout * flowLayout = [[UICollectionViewFlowLayout alloc] init];
        _collectionView  = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) collectionViewLayout:flowLayout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.scrollEnabled = NO;
        _collectionView.backgroundColor = [UIColor whiteColor];
        [_collectionView registerNib:[UINib nibWithNibName:@"HomeMainIconItemCollectionCell" bundle:nil] forCellWithReuseIdentifier:@"HomeMainIconItemCollectionCellID"];
    }
    return _collectionView;
}

@end
